﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace app
{
    static class Addresses
    {
        //public static byte[] CompactAddressRequest(byte[] address, byte[] peerAddress = null)
        //{
        //    //addr jumps bytes
        //    if (peerAddress == null)
        //        return new byte[1].Concat(address).ToArray();


        //    byte diff = (byte)Addresses.Compare(address.ToArray(), peerAddress);

        //    byte simm = Convert.ToByte(Parameters.addressSize - diff);

        //    return new byte[] { simm }.Concat(address.Skip(simm).Take(address.Count() - simm)).ToArray();
        //}

        //public static byte[] InflateAddressRequest(byte[] address)
        //{
        //    if (address.Length == 0)
        //        return address;

        //    byte[] result = new byte[address[0] + address.Count() - 1];

        //    Program.localPeer.Address.Take(address[0]).ToArray().CopyTo(result, 0);

        //    address.Skip(1).Take(address.Length - 1).ToArray().CopyTo(result, address[0]);

        //    return result;
        //}

        public static double Compare(byte[] s1, byte[] s2)
        {
            if (s1 == null || s2 == null)
                return Parameters.addressSize;

            byte[] a1 = new byte[Parameters.addressSize];
            byte[] a2 = new byte[Parameters.addressSize];

            s1.Take(Parameters.addressSize).ToArray().CopyTo(a1, 0);
            s2.Take(Parameters.addressSize).ToArray().CopyTo(a2, 0);

            int diff = 0;
            int simm = 0;

            for (int i = 0; i < Parameters.addressSize; i++)
            {
                if (a1[i] != a2[i])
                {
                    diff = Math.Abs(a1[i] - a2[i]);
                    break;
                }

                simm++;
            }

            return Parameters.addressSize - simm + ((double)diff / 1000);


            //var orig1 = new BitArray(a1);
            //var orig2 = new BitArray(a2);
            //var diff = orig1.Xor(orig2);

            //byte[] diffArray = new byte[Program.size];
            //diff.CopyTo(diffArray, 0);

            //int sum = 0;
            //int i = 0;
            //foreach (byte b in diffArray)
            //    sum += b * (i++ * 256);

            //return sum;
        }

        public static IPEndPoint FromBytes(byte[] endPoint)
        {
            if (endPoint[0] != 0)
                return new IPEndPoint(
                        new IPAddress(endPoint.Take(4).ToArray()),
                        BitConverter.ToUInt16(endPoint, Parameters.ipv4Addresssize));
            else
                return null;
        }

        public static byte[] ToBytes(IPEndPoint IPEndPoint)
        {
            return IPEndPoint.Address.GetAddressBytes().Concat(
                BitConverter.GetBytes((UInt16)IPEndPoint.Port)).ToArray();
        }
    }
}
